/*
 * MainFrame.java
 *
 * Created on 15.08.2009, 23:01:54
 */

package de.blitzcoder.cddatabase.gui;


import de.blitzcoder.cddatabase.data.CD;
import de.blitzcoder.cddatabase.data.Data;
import de.blitzcoder.cddatabase.data.Track;
import de.blitzcoder.cddatabase.gui.cdtable.CDTable;
import de.blitzcoder.cddatabase.gui.tracktable.TrackTable;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

/**
 *
 * @author blitzcoder
 */
public class MainFrame extends javax.swing.JFrame {

    private CDTable cdTable;
    private TrackTable trackTable;

    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
        addTables();
        updateStatistics();
    }

    private void addTables() {
        cdTable = new CDTable();
        cdTablePanel.add(new JScrollPane(cdTable));
        trackTable = new TrackTable();
        trackTablePanel.add(new JScrollPane(trackTable));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        cdTablePanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cdFilterTextField = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        newCdButton = new javax.swing.JButton();
        editCdButton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        trackTablePanel = new javax.swing.JPanel();
        openCDButton = new javax.swing.JButton();
        trackFilterTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cdCountLabel = new javax.swing.JLabel();
        trackCountLabel = new javax.swing.JLabel();
        durationLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(400, 400));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel2.setLayout(new java.awt.GridBagLayout());

        cdTablePanel.setLayout(new java.awt.GridLayout(1, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(cdTablePanel, gridBagConstraints);

        jLabel1.setText("Suche");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jLabel1, gridBagConstraints);

        cdFilterTextField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                cdFilterTextFieldCaretUpdate(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(cdFilterTextField, gridBagConstraints);

        jPanel5.setLayout(new java.awt.GridBagLayout());

        newCdButton.setText("Neue CD");
        newCdButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newCdButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel5.add(newCdButton, gridBagConstraints);

        editCdButton.setText("CD Editieren");
        editCdButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCdButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel5.add(editCdButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        jPanel2.add(jPanel5, gridBagConstraints);

        jTabbedPane1.addTab("CDs", jPanel2);

        jPanel4.setLayout(new java.awt.GridBagLayout());

        trackTablePanel.setLayout(new java.awt.GridLayout(1, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel4.add(trackTablePanel, gridBagConstraints);

        openCDButton.setText("CD öffnen");
        openCDButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openCDButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel4.add(openCDButton, gridBagConstraints);

        trackFilterTextField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                trackFilterTextFieldCaretUpdate(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel4.add(trackFilterTextField, gridBagConstraints);

        jLabel2.setText("Filter");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel4.add(jLabel2, gridBagConstraints);

        jTabbedPane1.addTab("Songs", jPanel4);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        getContentPane().add(jTabbedPane1, gridBagConstraints);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Statistik"));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel3.setText("CDs:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jLabel3, gridBagConstraints);

        jLabel4.setText("Songs:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jLabel4, gridBagConstraints);

        jLabel5.setText("Gesamtlänge:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jLabel5, gridBagConstraints);

        cdCountLabel.setText("jLabel6");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(cdCountLabel, gridBagConstraints);

        trackCountLabel.setText("jLabel7");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(trackCountLabel, gridBagConstraints);

        durationLabel.setText("jLabel8");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(durationLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        getContentPane().add(jPanel1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cdFilterTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_cdFilterTextFieldCaretUpdate
        cdTable.updateFilter(cdFilterTextField.getText());
    }//GEN-LAST:event_cdFilterTextFieldCaretUpdate

    private void editCdButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editCdButtonActionPerformed
        if (!cdTable.editCurrent(this))
            JOptionPane.showMessageDialog(this, "Keine CD ausgewählt!");
        else
            updateTables();
    }//GEN-LAST:event_editCdButtonActionPerformed

    private void trackFilterTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_trackFilterTextFieldCaretUpdate
        trackTable.updateFilter(trackFilterTextField.getText());
    }//GEN-LAST:event_trackFilterTextFieldCaretUpdate

    private void openCDButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openCDButtonActionPerformed
        if (!trackTable.editCurrentCD(this))
            JOptionPane.showMessageDialog(this, "Kein Song ausgewählt!");
        else
            updateTables();
    }//GEN-LAST:event_openCDButtonActionPerformed

    private void updateStatistics() {
        cdCountLabel.setText(Data.cdList.size()+"");
        trackCountLabel.setText(Data.getTrackCount()+"");

        String duration = Track.convertSecondsToString(Data.getDuration());
        String[] times = duration.split(":");
        if (times.length == 2) {
            durationLabel.setText(times[0]+" Minuten, "+times[1]+" Sekunden");
        } else if (times.length == 3) {
            durationLabel.setText(times[0]+" Stunden, "+times[1]+" Minuten, "+times[2]+" Sekunden");
        } else {
            // WTF?
        }
    }

    private void newCdButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newCdButtonActionPerformed
        CD cd = new CD();
        EditCDDialog dialog = new EditCDDialog(this, cd);
        dialog.setVisible(true);
        if (!cd.equals(new CD())) {
            Data.cdList.add(cd);
            updateTables();
        }
    }//GEN-LAST:event_newCdButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        try {
            Data.saveData();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Speichern fehlgeschlagen\n\n"+ex.toString());
            return;
        }
        dispose();
        System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    private void updateTables() {
        cdTable.updateCDList();
        trackTable.updateTrackList();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel cdCountLabel;
    private javax.swing.JTextField cdFilterTextField;
    private javax.swing.JPanel cdTablePanel;
    private javax.swing.JLabel durationLabel;
    private javax.swing.JButton editCdButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton newCdButton;
    private javax.swing.JButton openCDButton;
    private javax.swing.JLabel trackCountLabel;
    private javax.swing.JTextField trackFilterTextField;
    private javax.swing.JPanel trackTablePanel;
    // End of variables declaration//GEN-END:variables

}
